package com.xh.xyzp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.xyzp.common.constant.UserContext;
import com.xh.xyzp.common.domain.vo.resp.PageResp;
import com.xh.xyzp.common.enums.CommonStatusTypeEnum;
import com.xh.xyzp.domain.entity.Company;
import com.xh.xyzp.domain.entity.Deliver;
import com.xh.xyzp.domain.entity.Job;
import com.xh.xyzp.domain.entity.User;
import com.xh.xyzp.domain.vo.CompanyPageReq;
import com.xh.xyzp.domain.vo.CompanySubmitReq;
import com.xh.xyzp.enums.UserTypeEnum;
import com.xh.xyzp.exception.ApiException;
import com.xh.xyzp.mapper.CompanyMapper;
import com.xh.xyzp.service.CompanyService;
import com.xh.xyzp.service.DeliverService;
import com.xh.xyzp.service.JobService;
import com.xh.xyzp.service.UserService;
import com.xh.xyzp.util.SnowFlakeIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;
import java.util.Objects;

@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public void submit(CompanySubmitReq req) {
        req.checkParams();
        if (Objects.isNull(req.getId())) {
            Company company = BeanUtil.copyProperties(req, Company.class);
            company.setId(new SnowFlakeIdWorker(1, 0).nextId());
            company.setUserId(UserContext.getUser().getId());
            save(company);
        } else {
            Company company = getInfo(req.getId());
            BeanUtils.copyProperties(req, company);
            updateById(company);
        }
    }

    @Override
    public PageResp<Company> getList(CompanyPageReq req) {
        req.checkParams();
        LambdaQueryWrapper<Company> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(req.getStatus())) {
            lambdaQueryWrapper.eq(Company::getStatus, req.getStatus());
        }
        if (StrUtil.isNotEmpty(req.getName())) {
            lambdaQueryWrapper.like(Company::getName, req.getName());
        }
        Page<Company> companyPage = companyMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);
        return PageResp.<Company>builder()
                .list(companyPage.getRecords())
                .total(companyPage.getTotal())
                .build();
    }

    @Override
    public Company getInfo(Long companyId) {
        if (Objects.isNull(companyId)) {
            throw new ApiException("id不能为空");
        }
        return lambdaQuery()
                .eq(Company::getId, companyId)
                .eq(Company::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .one();
    }

    @Override
    public Company getCompanyByUserId(Long userId) {
        UserService userService = SpringUtil.getBean(UserService.class);
        User user = userService.getById(userId);
//        if (!user.getType().equals(UserTypeEnum.COMPANY.getKey())) {
//            throw new ApiException("不是公司用户，非法操作");
//        }
        Company company = companyMapper.selectOne(
                new LambdaQueryWrapper<Company>().eq(Company::getUserId, user.getId())
        );
        if (Objects.isNull(company)) {
            throw new ApiException("公司不存在");
        }
        return company;
    }

    @Override
    @Transactional
    public void delete(Long companyId) {
        UserService userService = SpringUtil.getBean(UserService.class);
        JobService jobService = SpringUtil.getBean(JobService.class);
        DeliverService deliverService = SpringUtil.getBean(DeliverService.class);
        Company company = companyMapper.selectById(companyId);
        companyMapper.deleteById(companyId);
        userService.removeById(company.getUserId());
        jobService.remove(new LambdaQueryWrapper<Job>().eq(Job::getCompanyId, companyId));
        deliverService.remove(new LambdaQueryWrapper<Deliver>().eq(Deliver::getCompanyId, companyId));
    }

}